const express=require('express')
const pool=require('../pool')
const router=express.Router()
module.exports=router
// 首页数据
router.get('/product',(req,res,next)=>{
	// 轮播图数据
	pool.query('SELECT * FROM su_index_carousel',(err,result1)=>{
		if(err){
			next(err)
			return
		}
		// 首页商品数据
		pool.query('SELECT pid,pro_id,title,price,pic FROM su_product where is_index=1',(err,result2)=>{
			if(err){
				next(err)
				return 
			}
			// 首页类别
			pool.query('SELECT * FROM su_product_family',(err,result4)=>{
				if(err){
					next(err)
					return
				}
				res.send({code:200,message:'操作成功',carouselItem:result1,ProductIndex:result2,productFamily:result4})
			})
		})
	})
})
// 首页搜索
router.get('/search',(req,res,next)=>{
	let { kw,pno,pagenum }=req.query
	pagenum = pagenum ? Number(pagenum) : 10
	const start = (pno-1)*pagenum
	pool.query('SELECT * FROM su_product WHERE title like ? LIMIT ?,?',[`%${kw}%`,start,pagenum],(err,result)=>{
		if(err){
			next(err)
			return 
		}
		pool.query('SELECT count(*) count FROM su_product WHERE title like ?',[`%${kw}%`],(err,result2)=>{
			if(err){
				next(err)
				return
			}
			res.send({Code:200,statusCode:'OK',list:result,count:result2[0].count,pageCount:Math.ceil(result2[0].count/pagenum),pno:Number(pno)})
		})
	})
})